package org.lib.rabbitmqhello.try4;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig4 {
	
	public final static String QUEUE_4 = "queue4";
	public final static String EXCHANGE_4 = "ex4";
	
	@Bean
	public Queue queue4() {
		Queue queue4 = new Queue(QUEUE_4);
		return queue4;
	}
	
	@Bean
	public DirectExchange directExchange4() {
		return new DirectExchange(EXCHANGE_4);
	}
	
	@Bean
	public Binding binding4(Queue queue4, DirectExchange directExchange4) {
		return BindingBuilder
				.bind(queue4)
				.to(directExchange4)
				.withQueueName();
	}
	
	// 批量消费
	@Bean
	public SimpleRabbitListenerContainerFactory batchRabbitListenerContainerFactory4(ConnectionFactory cf) {
		SimpleRabbitListenerContainerFactory f = new SimpleRabbitListenerContainerFactory();
		f.setConnectionFactory(cf);
		
		// prefetch值
		f.setPrefetchCount(5);
//		f.setPrefetchCount(15);
		
		// 设置批量
		f.setBatchListener(true);
		f.setConsumerBatchEnabled(true);
//		f.setBatchSize(5);
		
		// 大于 prefetch值
		// 此时，消费者 的 prefetch 变为的 15
		f.setBatchSize(15);
		
		return f;
	}
}
